package cn.hpclub.server.model;

import java.util.ArrayList;
import java.util.List;

import com.jfinal.ext.plugin.tablebind.TableBind;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

import cn.hpclub.server.constant.Constant;
import cn.hpclub.server.service.PaymentService;

/**
 * 实体类 - 管理员角色
 * 
 */
@TableBind(tableName = "organization_class")
public class Website extends Model<Website>{

    private static final long   serialVersionUID = 626637562061751242L;
    public static final Website dao              = new Website();

    public final static String  ID               = "id";               // 主键
    public final static String  WEBSITE_ID       = "WebsiteId";        //
    public final static String  WEBSITE_NAME     = "class_name";       //
    public final static String  CLASS_ID         = "class_id";         //
    public final static String  MODULEID         = "module_id";        //
    public final static String  ORG_ID           = "org_id";           //
    public final static String  THEMES_NAME      = "name";             //
    public final static String  ENABLED          = "enabled";          //
    public final static String  APPID            = "app_id";           //

    public Page<Record> list(int number, int size, String class_name){
        try{
            List<Object> p = new ArrayList<Object>();
            StringBuffer sql_select = new StringBuffer();
            StringBuffer sql_from = new StringBuffer();
            sql_select.append("SELECT *");
            sql_from.append(" from " + Constant.Table.ORGANIZATION_CLASS + " where " + WEBSITE_NAME + " like ?");

            p.add("%" + class_name + "%");
            return Db.paginate(number, size, sql_select.toString(), sql_from.toString(), p.toArray());
        }
        catch(Exception e){
            e.printStackTrace();
            return null;
        }
    }

    public List<Record> getWebsite(boolean check_enabled){
        try{

            if(check_enabled){

                return Db.find("select * from " + Constant.Table.ORGANIZATION_CLASS + " where " + ENABLED + "='1'");
            } else{
                return Db.find("select * from " + Constant.Table.ORGANIZATION_CLASS);
            }

        }
        catch(Exception e){
            e.printStackTrace();
            return null;
        }
    }

    public boolean checkUnique(int id, int websiteid){
        try{

            List<Record> result = Db
                    .find("select " + ID + " from " + Constant.Table.ORGANIZATION_CLASS + " where " + WEBSITE_ID + "=?",
                          new Object[] { websiteid });

            if(result.size() <= 0 || result.get(0).getInt("id") == id){

                return true;
            }

            return false;

        }
        catch(Exception e){
            e.printStackTrace();

            return true;
        }
    }

    /**
     * @Title: queryWebsite
     * @Description:根据是精确查询还是模糊查询来搜索不同信息（当前业务的支付方式已经配置过除外）
     * @param pageNumber
     * @param pageSize
     * @param keyword
     * @param queryType
     * @param payId
     * @param moduleId
     * @param cityOrgId
     * @return Page<Record>
     */
    public Page<Record> queryWebsite(int pageNumber, int pageSize, String keyword, String queryType, int moduleId,
            int payId, int cityOrgId){
        try{
            List<Object> p = new ArrayList<Object>();
            StringBuffer sql_select = new StringBuffer();
            StringBuffer sql_from = new StringBuffer();

            String operate = " LIKE ";
            String args = "%" + keyword + "%";
            if("exact".equals(queryType)){
                operate = " = ";
                args = keyword;
            }

            sql_select.append("SELECT *");
            sql_from.append(" FROM " + Constant.Table.ORGANIZATION_CLASS + " oc WHERE " + WEBSITE_NAME + operate
                    + " ?");
            sql_from.append(" AND oc.id NOT IN ( SELECT pc.website_id FROM pay_config pc WHERE pc.module_id = ? AND pc.pay_plat = ? AND pc.city_org_id = ? ) ");

            p.add(args);
            p.add(moduleId);
            p.add(payId);
            p.add(cityOrgId);

            return Db.paginate(pageNumber, pageSize, sql_select.toString(), sql_from.toString(), p.toArray());
        }
        catch(Exception e){
            e.printStackTrace();
            return null;
        }
    }

    /**
     * @Title: getPays
     * @Description:获取除建行支付方式和支付宝支付以外的支付方式
     * @param moduleId：业务模块编码：1-充值缴费、3-选号入网（sales_promotion表可查）
     * @return List<Record>
     */
    public List<Record> getPays(int moduleId){
        return Db.find("select * from pay where code <> " + PaymentService.PAYMENT_CCB + " and code <> "
                + PaymentService.PAYMENT_ALIPAY);// 获取除建行支付方式和支付宝支付以外的支付方式
        // return Db.find("select * from pay where code <> " +
        // PaymentService.PAYMENT_CCB
        // + (moduleId == PaymentService.MODULE_ID_CHONG_ZHI ? " and code <> " +
        // PaymentService.PAYMENT_ALIPAY
        // : ""));// 获取除建行支付方式以外的支付方式（目前是支付宝支付和银联支付），充值缴费只有银联支付
    }

    /**
     * @Title: getPayments
     * @Description:获取地市可授权的支付方式（即除建行支付方式和无效的支付方式以外的方式）
     * @param moduleId2
     * @return List<Record>
     */
    public List<Record> getPayments(int moduleId2){
        return Db.find("select * from pay where enabled=1 and code <> " + PaymentService.PAYMENT_CCB);
    }

}
